package com.qfedu.fmshop.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.fmshop.common.vo.LoginVo;
import com.qfedu.fmshop.common.vo.ResultVO;
import com.qfedu.fmshop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@CrossOrigin  //允许跨域请求的注解
@RestController
@RequestMapping("/user")
@Api(value = "提供用户的登录和注册接口",tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username", value = "用户登录账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password", value = "用户登录密码",required = true)
    })
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultVO login(@RequestBody String param){

        //处理参数
        log.info("用户登录userController接收参数为：{}",param);
        LoginVo loginVo = JSON.parseObject(JSON.parseObject(param).getString("params"),LoginVo.class);
        System.out.println(loginVo);

        //调用service层
        ResultVO resultVO = userService.CheckLogin(loginVo);

        log.info("用户登录userController向前端返回参数:{}",resultVO);
        //返回结果
        return resultVO;
    }

    @ApiOperation("用户注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username", value = "用户注册账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password", value = "用户注册密码",required = true)
    })
    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public ResultVO regist(@RequestBody String param){
//        System.out.println("username--->"+username);
//        System.out.println("password--->"+password);
        log.info("用户注册UserController接收参数：{}",param);
        String username = JSON.parseObject(param).getString("username");
        String password = JSON.parseObject(param).getString("password");

        //调用注册逻辑
        ResultVO resultVO = userService.UserRegistration(username,password);


        return resultVO;
    }
}
